package gfyx_user_service_remote

import (
	"context"
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/common/tenant_db"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/infrastructure/common/constants"
	gfyxUserServicePb "git.mycaigou.com/gfyx/micro-gfyx-oppty-service/proto/micro_gfyx_user_service_proto/micro-gfyx-user-service"
	tenantPb "git.mycaigou.com/gfyx/micro-gfyx-oppty-service/proto/micro_gfyx_user_service_proto/micro-gfyx-user-service/tenant"
)

type userRepository struct {
	tenant_db.TenantDB
}

// NewGfyxUserServiceRepository 构造函数
func NewGfyxUserServiceRepository() GfyxUserServiceRepositoryIface {
	return &userRepository{}
}

// IncrementSyncUser 增量同步用户
func (r *userRepository) IncrementSyncUser(ctx context.Context, request *gfyxUserServicePb.IncrementSyncUserRequest) (*gfyxUserServicePb.IncrementSyncUserResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MicroGfyxUserService)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := gfyxUserServicePb.NewUserInfoServiceClient(conn)
	res, err := client.IncrementSyncUser(ctx, request)
	if err != nil {
		return nil, err
	}

	return res, nil
}

// GetTenantByName 根据名称获取租户信息
func (r *userRepository) GetTenantByName(ctx context.Context, request *tenantPb.GetTenantByNameRequest) (*tenantPb.GetTenantByNameResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MicroGfyxUserService)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := tenantPb.NewTenantServiceClient(conn)
	res, err := client.GetTenantByName(ctx, request)
	if err != nil {
		return nil, err
	}

	return res, nil
}

// GetTenantByCode 根据code获取租户信息
func (r *userRepository) GetTenantByCode(ctx context.Context, request *tenantPb.GetTenantByCodeRequest) (*tenantPb.GetTenantByCodeResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MicroGfyxUserService)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := tenantPb.NewTenantServiceClient(conn)
	res, err := client.GetTenantByCode(ctx, request)
	if err != nil {
		return nil, err
	}

	return res, nil
}

// GetAllEfficientTenant 获取有效的租户列表
func (r *userRepository) GetAllEfficientTenant(ctx context.Context, request *tenantPb.GetAllEfficientTenantRequest) (*tenantPb.GetAllEfficientTenantResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MicroGfyxUserService)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := tenantPb.NewTenantServiceClient(conn)
	res, err := client.GetAllEfficientTenant(ctx, request)
	if err != nil {
		return nil, err
	}

	return res, nil
}

func (r *userRepository) GetIndustryByNames(ctx context.Context, request *gfyxUserServicePb.GetIndustryByNamesRequest) (*gfyxUserServicePb.GetIndustryByNamesResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MicroGfyxUserService)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := gfyxUserServicePb.NewStrategicMonitorServiceClient(conn)
	res, err := client.GetIndustryByNames(ctx, request)
	if err != nil {
		return nil, err
	}

	return res, nil
}

func (r *userRepository) AddIndustry(ctx context.Context, request *gfyxUserServicePb.AddIndustryRequest) (*gfyxUserServicePb.AddIndustryResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MicroGfyxUserService)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := gfyxUserServicePb.NewStrategicMonitorServiceClient(conn)
	res, err := client.AddIndustry(ctx, request)
	if err != nil {
		return nil, err
	}

	return res, nil
}

func (r *userRepository) GetBidCompanyByCodes(ctx context.Context, request *gfyxUserServicePb.GetBidCompanyByCodesRequest) (*gfyxUserServicePb.GetBidCompanyByCodesResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MicroGfyxUserService)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := gfyxUserServicePb.NewStrategicMonitorServiceClient(conn)
	res, err := client.GetBidCompanyByCodes(ctx, request)
	if err != nil {
		return nil, err
	}

	return res, nil
}

func (r *userRepository) AddBidCompany(ctx context.Context, request *gfyxUserServicePb.AddBidCompanyRequest) (*gfyxUserServicePb.AddBidCompanyResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MicroGfyxUserService)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := gfyxUserServicePb.NewStrategicMonitorServiceClient(conn)
	res, err := client.AddBidCompany(ctx, request)
	if err != nil {
		return nil, err
	}

	return res, nil
}

func (r *userRepository) GetMonitorResult(ctx context.Context, request *gfyxUserServicePb.GetMonitorResultRequest) (*gfyxUserServicePb.GetMonitorResultResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MicroGfyxUserService)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := gfyxUserServicePb.NewStrategicMonitorServiceClient(conn)
	res, err := client.GetMonitorResult(ctx, request)
	if err != nil {
		return nil, err
	}

	return res, nil
}
